package com.ruoyi.web.utils;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.AlgorithmParameters;
import java.security.Security;


/**
 * AES加密解密工具类
 * @author zdh
 * @date 2020-06-29
 */
public class AESUtils {

    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    /**
     * 解密
     * @param data
     * @param key
     * @param iv
     * @return
     * @throws Exception
     */
    public static String decrypt(String data, String key, String iv) throws Exception {
        byte[] dataByte = Base64.decodeBase64(data);
        byte[] keyByte = Base64.decodeBase64(key);
        byte[] ivByte = Base64.decodeBase64(iv);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyByte, "AES");
        AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
        parameters.init(new IvParameterSpec(ivByte));
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, parameters);
        byte[] resultByte = cipher.doFinal(dataByte);
        String result = new String(resultByte,"UTF-8");
        return result;
    }

    /*public static void main(String[] args) throws NoSuchPaddingException, UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
        String ciphertext = "o0Pgl/5LeD9LGg20ikNrveru4tiun6FB1Ta/X9j3Zu/XyE99tFQOcXqoSdHE9CrWxXzd71iPeaeVJ/VFzmkBRX5TCOvxgySEkXhwepbdGqsHtO59hCa1mHxli2ZpBdJEqxK2cboWapjRj/9GYi6G4QPr14Wxko5pKRnB1my2Bxrx1zD+nK7X/8kDV7winjFwrzpMT44rxJtqKFe7jSZisI7VbOYmdrsjcZWBSDuwTGBnDo5MKOpKbddwiXBZkjDOPbXRE6CFULsjS7ePE4SsqhKyh18+27cv8fSHW4CuMTZ3alLv9Vj7vQYqrEoU/zIm/RK2FO9mHzfUlDXjaIsid2bfNjV3Wpq65OmuWTIfIdQISRRsfFP0nThJifkfgmjp48O3y2xlNYW++I+mKI7n0w9A/y7AjvC1iFz/ZLGmER6gkvyEoIt970s2HAgiHRGt6DulIQKt6vS4fuXNtgXF5TkYMbX9c/ObbiQLNEuCch/XLMV/AT4mCLZMKrdmkeIW/BrnXyrb5jdoZRQBZ+mdqUtXkyoS8aTahmUnI5t/YzS6HogsidTQFcOmZGZfqdj6tBvdVbaZXdHRTqDBZ4nq0dSuzqjmPOJ8w23I/JuBjITcnQJXXLvvDzGnnyEE+ttpHYTreDp1VcjKBZkAy0MWCuZSqDDSpKs5dIiMnW4HsTtaX+HoLkPZgvlolGTP78gdA0XpkEuL446aT3X9KrHbnQSmjBiIzBhqRujQdZQPkk5XFv7esb4jS1exnpB1gLiBddzCMbj2jkgbbnZVQ/rlPVCnqMyw2c36aTfqlpNoS2qOndBr724gBLE3S4ehWBXoC30ZZFAzo9WXqxJRzrazqEdNpFYN5Gi0bbDnrQ41H6o/n7Gx2uZCiG1mDQ0BVw/0tLXSuxjMrlSCK7utPvETvWuwpEWYniC2dgGfW3UD67Aoh0Q3TV6LbRQ=";
        String associated_data = "payscore";
        String nonce = "QMJ1yphbBHvL";

        String context = weChatAPIV3Aes(associated_data,nonce,ciphertext, TimvvaConfig.API_V3_KEY);

        System.out.println(context);
    }*/

}
